package com.rpoonolly.simplegame.processors;

import java.util.List;

import javax.vecmath.Vector2d;

import com.rpoonolly.simplegame.Config;
import com.rpoonolly.simplegame.SimpleUserAction;
import com.rpoonolly.simplegame.core.CollisionDetector;
import com.rpoonolly.simplegame.core.CollisionResponder;
import com.rpoonolly.simplegame.core.InputManager;
import com.rpoonolly.simplegame.core.aabb.AABBCollision;
import com.rpoonolly.simplegame.core.aabb.AABBEntity;
import com.rpoonolly.simplegame.core.aabb.DynamicAABBEntity;
import com.rpoonolly.simplegame.core.aabb.EntityGameMap;

/**
 * @author Ryan
 * 
 * class to handle basic player movement. Moves player based on input collides against worldObjs.
 */
public class SimplePlayerProcessor extends DynamicEntityProcessor {
	protected InputManager<SimpleUserAction> inputMngr;
	
	public SimplePlayerProcessor(
		CollisionDetector<AABBEntity, AABBCollision> detector,
		CollisionResponder<AABBCollision> responder,
		EntityGameMap<AABBEntity> gameMap,
		InputManager<SimpleUserAction> inputMngr
	) {
		super(detector, responder, gameMap);
		this.inputMngr = inputMngr;
	}

	@Override
	public void process() {
		processTime();
		
		List<AABBEntity> worldObjs = entities.get(Config.ENT_GRP_WORLD_OBJS);
		DynamicAABBEntity player = (DynamicAABBEntity) entities.get(Config.ENT_GRP_PLAYER).get(0);
		
		Vector2d v = player.getDirection();
		v.x = 0;
		v.y = 0;
		
		if (inputMngr.isActionRequested(SimpleUserAction.PLAYER_MOVE_UP)) {
			v.y = Config.PLAYER_SPEED * dt / 1000;
		} else if (inputMngr.isActionRequested(SimpleUserAction.PLAYER_MOVE_DOWN)) {
			v.y = -1 * Config.PLAYER_SPEED * dt / 1000;
		}
		
		if (inputMngr.isActionRequested(SimpleUserAction.PLAYER_MOVE_LEFT)) {
			v.x = -1 * Config.PLAYER_SPEED * dt / 1000;
		} else if (inputMngr.isActionRequested(SimpleUserAction.PLAYER_MOVE_RIGHT)) {
			v.x = Config.PLAYER_SPEED * dt / 1000;
		}
		
		AABBCollision minCollision = checkGroupCollision(player, worldObjs);
		if (minCollision != null && !Config.isEditModeEnabled.get())
			v = collisionResponder.collisionRespond(minCollision);
		
		player.setX((int) (player.getX() + v.x));
		player.setY((int) (player.getY() + v.y));
	}
}